package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.DiscountCategory;
import com.yuyou.fn.educational.entity.DiscountRule;
import com.yuyou.fn.educational.mapper.DiscountCategoryMapper;
import com.yuyou.fn.educational.service.IDiscountCategoryService;
import com.yuyou.fn.educational.service.IDiscountRuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *
 * DiscountCategory 表数据服务层接口实现类
 *
 */
@Service
public class DiscountCategoryServiceImpl extends SuperServiceImpl<DiscountCategoryMapper, DiscountCategory,Long> implements IDiscountCategoryService {

    @Resource
    private IDiscountRuleService discountRuleService ;

    @Override
    public Page<DiscountCategory> findDiscountCategory(int pageNo, int pageSize) {
        Example<DiscountCategory> example = DiscountCategory.createExample();
        example.orderBy("createTime").desc();
        return findPageByExample(example ,pageNo,pageSize);
    }

    @Override
    public void delCategory(Long discountCategoryId) {
        DiscountCategory discountCategory = findById(discountCategoryId);
        if(discountCategory.getIsBuildIn() == 1 ){
            throw new BusException("内置分类不能删除");
        }

        Example<DiscountRule> example = DiscountRule.createExample();
        example.createCriteria().andEqualTo("categoryId" ,discountCategory );
        long l = discountRuleService.countByExample(example);
        if(l > 0 ){
            throw new BusException("分类已经被使用，无法删除");
        }

        deleteById(discountCategoryId);

    }

    @Override
    public void saveOrUpdateCategory(Long discountCategoryId, String name) {
        name=name.trim();

        if(discountCategoryId == null ){

            DiscountCategory discountCategory = new DiscountCategory();
            discountCategory.setName(name);
            discountCategory.setIsBuildIn(0);
            discountCategory.setCreateTime(TimeUtils.now());
            discountCategory.setUpdateTime(TimeUtils.now());
            this.checkDiscountCategoryName(discountCategory);
            insertSelective(discountCategory);

        }else{
            DiscountCategory discountCategory = new DiscountCategory();
            discountCategory.setDiscountCategoryId(discountCategoryId);
            discountCategory.setName(name);
            this.checkDiscountCategoryName(discountCategory);
            updateSelectiveById(discountCategory);
        }

    }

    void  checkDiscountCategoryName(DiscountCategory discountCategory){
        if (StringUtils.isEmpty(discountCategory.getName())){
            throw new BusException("名称不可为空");
        }
        Example<DiscountCategory> example = DiscountCategory.createExample();
        example.createCriteria().andEqualTo("name",discountCategory.getName());
        DiscountCategory check=this.findOneByExample(example);
        if (check!=null && !check.getDiscountCategoryId().equals(discountCategory.getDiscountCategoryId())){
            throw new BusException("该分类名称已存在");
        }
    }

    @Override
    public String findCategoryNameByCategoryId(Long categoryId) {
        DiscountCategory dis = findById(categoryId);
        return dis == null ? StringUtils.EMPTY : dis.getName() ;
    }
}